# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/8/19
import logging
import os
from logging import handlers
from pathlib import Path

from submodules.common.config.log_ import LOG_LEVEL
from submodules.common.lib.local_paths import project_tmp_log_path


def get_logger(log_filename="common.log", log_dir=project_tmp_log_path):
    p = Path(log_filename)
    log_filename = p.with_suffix(".log").name
    # 创建日志文件
    log_path = os.path.join(log_dir, log_filename)
    if os.path.exists(log_path) == False:
        f = open(log_path, "w", encoding="utf-8")
        f.close()

    log_conf_basic = {
        # "filename": "install.log",
        # "mode": "w",
        # "format": '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
        "format": '%(asctime)s.%(msecs)d [%(levelname)s] %(message)s [%(funcName)s()][%(filename)s][line:%(lineno)d]',
        "datefmt": '%Y-%m-%d %H:%M:%S',
        "level": logging.INFO,
    }

    RotatingFileHandler_config = {
        "filename": log_path,
        "mode": "a",
        "maxBytes": 1 * 1024 * 1024,
        "backupCount": 10,
        "encoding": "utf-8"
    }

    handlers_list = []
    handler = logging.StreamHandler()
    handlers_list.append(handler)

    handler = handlers.RotatingFileHandler(**RotatingFileHandler_config)
    handlers_list.append(handler)

    logging.basicConfig(**log_conf_basic, handlers=handlers_list)
    logging.basicConfig()
    logger = logging.getLogger()
    logger.setLevel(LOG_LEVEL)
    return logger

logger = get_logger()

if __name__ == "__main__":
    pass
